package com.type.TypingDetector;

import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;

/**
 * This class adds multiple entries to the database and pulls them back
 * out.
 */
public class DatabaseQuery {
	// Variables area
	private ArrayList<String> arrayKeys = null;
	private ArrayList<String> arrayValues = null;
	private ArrayList<String> databaseKeys = null;
	private ArrayList<String> databaseKeyOptions = null;
	private DBAdapter database;
	
	/**
	 * Initialize the ArrayList
	 * @param context Pass context from calling class.
	 */
	public DatabaseQuery(Context context) {
		// Create an ArrayList of keys and one of the options/parameters
		// for the keys.
		databaseKeys = new ArrayList<String>();
		databaseKeyOptions = new ArrayList<String>();
		databaseKeys.add("Title");
		databaseKeyOptions.add("text not null");

		// Call the database adapter to create the database
		database = new DBAdapter(context, "testTable", databaseKeys, databaseKeyOptions);
        database.open();
		arrayKeys = new ArrayList<String>();
		arrayValues = new ArrayList<String>();

	}
	
	/**
	 * Initialize the ArrayList
	 * @param context Pass context from calling class.
	 */
	public DatabaseQuery(Context context, ArrayList<String> dbKeys, ArrayList<String> dbOpts) {
		// Create an ArrayList of keys and one of the options/parameters
		// for the keys.
		databaseKeys = dbKeys;
		databaseKeyOptions = dbOpts;
		//databaseKeys.add("Title");
		//databaseKeyOptions.add("text not null");

		// Call the database adapter to create the database
		database = new DBAdapter(context, "testTable5", databaseKeys, databaseKeyOptions);
        database.open();
        //database.clearTable();
		arrayKeys = new ArrayList<String>();
		arrayValues = new ArrayList<String>();

	}
	
	/**
	 * Initialize the ArrayList
	 * @param context Pass context from calling class.
	 */
	public DatabaseQuery(Context context, ArrayList<String> dbKeys, ArrayList<String> dbOpts, String tabName) {
		// Create an ArrayList of keys and one of the options/parameters
		// for the keys.
		databaseKeys = dbKeys;
		databaseKeyOptions = dbOpts;
		//databaseKeys.add("Title");
		//databaseKeyOptions.add("text not null");

		// Call the database adapter to create the database
		database = new DBAdapter(context, tabName, databaseKeys, databaseKeyOptions);
        database.open();
        //database.clearTable();
		arrayKeys = new ArrayList<String>();
		arrayValues = new ArrayList<String>();

	}
	
	/**
	 * Append data to an ArrayList to then submit to the database
	 * @param key Key of the value being appended to the Array.
	 * @param value Value to be appended to Array.
	 */
	public void appendData(String key, String value){
		arrayKeys.add(key);
		arrayValues.add(value);
	}

	/**
	 * This method adds the row created by appending data to the database.
	 * The parameters constitute one row of data.
	 */
	public void addRow(){
		database.insertEntry(arrayKeys, arrayValues);
		//arrayKeys.clear();
		//arrayValues.clear();
	}
	
	public void deleteRow(long rowID) {
		database.removeEntry(rowID);
	}
	
	public boolean deleteRow(String[] colTitle, String rowTitle) {
		Cursor results=database.getAllEntries(colTitle, null, null, null, null, null);
		long rowID=-1;
		while(results.moveToNext()) {
			if (results.getString(results.getColumnIndex(colTitle[1])).equalsIgnoreCase(rowTitle)) {
				rowID=results.getLong(results.getColumnIndex(colTitle[0]));
			}
		}
		if (rowID>=0) {
			return database.removeEntry(rowID);
			//database.setTransactionSuccesful();
		}
		return false;
	}
	
	/**
	 * Get data from the table.
	 */
	public ArrayList<String> getData(String[] keys, String selection, String[]
	  selectionArgs, String groupBy, String having, String sortBy, String sortOption){

		ArrayList<String> list = new ArrayList<String>();
		Cursor results = database.getAllEntries(keys, selection,
				selectionArgs, groupBy, having, sortBy, sortOption);
		while(results.moveToNext())
			list.add(results.getString(results.getColumnIndex(sortBy)));
		return list;

	}
	
	/**
	 * Get data from the table.
	 */
	public ArrayList<String> getData(String[] keys, String selection, String[]
	  selectionArgs, String groupBy, String having, String sortBy){

		ArrayList<String> list = new ArrayList<String>();
		Cursor results = database.getAllEntries(keys, selection,
				selectionArgs, groupBy, having, sortBy);
		while(results.moveToNext())
			list.add(results.getString(results.getColumnIndex(sortBy)));
		return list;

	}
	
	/**
	 * Get data from the table. Sorts by first column passed in
	 */
	public ArrayList<String> getDataNS(String[] keys, String selection, String[]
	  selectionArgs, String groupBy, String having){

		ArrayList<String> list = new ArrayList<String>();
		Cursor results = database.getAllEntries(keys, selection,
				selectionArgs, groupBy, having, null);
		while(results.moveToNext())
			for (int i=0; i<keys.length; i++) {
				list.add(results.getString(results.getColumnIndex(keys[i])));
			}
		return list;

	}
	
	/**
	 * Get data from the table.
	 */
	/*public ArrayList<DummyDataObject> getDataD(String[] keys, String selection, String[]
	  selectionArgs, String groupBy, String having, String sortBy){

		ArrayList<DummyDataObject> list = new ArrayList<DummyDataObject>();
		Cursor results = database.getAllEntries(keys, selection,
				selectionArgs, groupBy, having, sortBy);
		String[] stuff=new String[keys.length];
		while(results.moveToNext()) {
			for (int i=0; i<keys.length; i++) {
				stuff[i]=results.getString(results.getColumnIndex(keys[i]));
			}
			list.add(new DummyDataObject(stuff));
			//list.add(results.getString(results.getColumnIndex(sortBy)));
		}
		return list;

	}*/
	
	/**
	 * Destroy the reporter.
	 * @throws Throwable
	 */
	public void destroy() throws Throwable{
        database.close();
	}
	
	public void clearTable() {
		database.clearTable();
	}
}

